#include "ChatDialog.h"

int ChatDialog::instance = 0;

ChatDialog::ChatDialog( Board * newBoard, QWidget * parent):QMainWindow(parent)
{
    if (newBoard == 0)
	return;
    DEBUG_F();
    instance ++;
    board = newBoard;
//     topic = 0;
    messageEdit = 0;
    articleList = new ArticleList(this);
    setAttribute(Qt::WA_DeleteOnClose);
    
    setupUi(this);
    this -> setWindowTitle (board -> name);

    bmodel = new boardModel(this);
    bmodel -> setBoard(board);
    topicList -> setModel(bmodel);
    topicList -> setAlternatingRowColors(true);
    /*
    tmodel = new topicModel(this);
    articleList -> setModel(tmodel);
    articleList -> setAlternatingRowColors(true);
    */
    articleArea->setWidget(articleList);
    articleList->show();
    readSettings();
    updateList();

    connect(board, SIGNAL(refreshFinished(bool)), this, SLOT(updateList(bool)));
}

void ChatDialog::on_topicList_clicked(const QModelIndex & index){
    DEBUG_F();
    Topic * topic = articleList->getTopic();
    if (topic)
	disconnect(&gFastTimer, SIGNAL(timeout()), topic, SLOT(refreshMe()));
    topic = static_cast<Topic *>(index.internalPointer());
    articleList ->setTopic(topic);
    connect(&gFastTimer, SIGNAL(timeout()), topic, SLOT(refreshMe()));
}

void ChatDialog::on_articleList_clicked(const QModelIndex & /*index*/){
    DEBUG_F();
    /*
    Article * article = static_cast<Article *>(index.internalPointer());
    Q_CHECK_PTR(article);
    QString prefix = tr("Re: ");
    if (article->title.indexOf(prefix) > -1) prefix = "";
    QString title = prefix + article->title;
    QString text = article->reply();
    qDebug() << title;
    if (messageEdit == 0){
	messageEdit = new MessageEdit(board, title, this);
    }
    messageEdit -> set(title, text);
    */
}

void ChatDialog::on_actionReply_triggered(){
    DEBUG_F();
    /*
    QString title(tr("New Post"));
    if (topic) title = tr("Re: ")+topic ->name;
    if (messageEdit == 0){
	messageEdit = new MessageEdit(board, title, this);
    }
    else
	messageEdit -> set(title);
    */
}

void ChatDialog::on_actionFresh_triggered(){
    DEBUG_F();
    board-> refreshMe();
}

void ChatDialog::updateList(bool error){
    DEBUG_F();
    if (error)
	return;
    topicList -> reset();
    if (articleList->getTopic() == 0){
	// initialed, we show the newest topic
	int i = board->topicPool.count();
	if ( i == 0) return;
	Topic * topic = board->topicPool.value(i-1);
	articleList ->setTopic(topic);
// 	topicList->setCurrentRow(i-1);
	connect(&gFastTimer, SIGNAL(timeout()), topic, SLOT(refreshMe()));
    }
}

void ChatDialog::closeEvent(QCloseEvent * event){
    DEBUG_F();
    instance --;
    hide();
    writeSettings();
    delete articleList;
    disconnect(board, SLOT(refreshMe()));
    event-> accept();
}

void ChatDialog::writeSettings(){
    DEBUG_F();
    QSettings settings (PROGRAM_COMP, PROGRAM_NAME);
    settings.beginGroup("ChatDialog");
    settings.setValue("geometry", saveGeometry());
    settings.endGroup();
}

void ChatDialog::readSettings(){
    DEBUG_F();
    QSettings settings (PROGRAM_COMP, PROGRAM_NAME);
    settings.beginGroup("ChatDialog");
    restoreGeometry(settings.value("geometry").toByteArray());
    settings.endGroup();
    move(x()+ 30* instance- 30, y()+ 30* instance- 30 );
}
